<?php
namespace app\home\controller;

use think\facade\App;
use think\facade\Hook;
use think\facade\Request;

class Domain extends Base {
    
    protected function initialize()
    {
        parent::initialize();
        $this->checkLogin();
    }
    
    /**
     * @desc 我的应用
     */
    public function index() {
        $params['pageSize'] = input('pageSize', 8);
        $params['userId']   = $this->userId;
    
        $logic = App::model('Domain', 'logic');
        $data = $logic->index($params);

        $this->assign($data);
        return view();
    }
    
    /**
     * @desc 我的应用
     */
    public function add() {
        return view();
    }
    
    /**
     * @desc 添加域名
     */
    public function operateAjax() {
        Hook::listen('repeat_request', $this->userId);
        
        $params['httpProtocol'] = Request::post('httpProtocol');
        $params['domainName']   = Request::post('domainName');
        $params['httpsPem']     = Request::post('httpsPem');
        $params['httpsKey']     = Request::post('httpsKey');
        
        $logic = model('Domain', 'logic');
        $data = [];
        try {
            $data = $logic->operateAjax($params, $this->userId);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }
    
    /**
     * @desc 删除域名
     */
    public function del() {
        Hook::listen('repeat_request', $this->userId);
    
        $params['id'] = Request::post('id');
    
        $logic = model('Domain', 'logic');
        $data = [];
        try {
            $data = $logic->del($params, $this->userId);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
}
